
#include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
#include <QMessageBox>
#include <QDir>
#include "qrencode.h"
#include "qrenc.c"

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);
    QApplication::setApplicationDisplayName(QObject::tr("QRCode"));
    QApplication::setApplicationVersion(QStringLiteral("1.0"));
    QCommandLineParser parser;      // 命令行解析器
    parser.addHelpOption();         // 设置帮助选项

    parser.addPositionalArgument(QObject::tr("[Text]"),
                                 QObject::tr("条码内容"));
    parser.addPositionalArgument(QObject::tr("[FileName]"), QObject::tr("条码图片保存路径及文件名"));

//    QCommandLineOption displayOnlyOption(QStringList() << "d" << "display_only", "只显示, 不能修改.");
//    parser.addOption(displayOnlyOption);

    parser.process(QCoreApplication::arguments());

    qDebug() << "Arguments size:" << parser.positionalArguments().size();
    if (parser.positionalArguments().size() < 1) {
        QMessageBox::critical(0,QObject::tr("Critical"),
                              QObject::tr("调用参数不正确!\n%1").arg(parser.helpText()));
        parser.showHelp(-1);
    }

    QString s_text = parser.positionalArguments().at(0);
    QString s_fileName = parser.positionalArguments().at(1);
    qDebug() << s_text;
    qDebug() << s_fileName;
    qDebug() << QDir::fromNativeSeparators(s_fileName);

    if (s_text.isEmpty()) {
        return 0;
    }
    if (s_fileName.isEmpty()){
        s_fileName = app.applicationDirPath().append(QLatin1Literal("\output.png"));
    }
    char outfile[] = "output.png";
    margin = 2;
    ::size = 7;
    version = 2;
    //QRcode *qrcode = QRcode_encodeString(info.toStdString().data(), 2, QR_ECLEVEL_L, QR_MODE_8, 0);
    //writePNG(qrcode, outfile);

    qrencode((unsigned char *)s_text.toStdString().data(), s_text.length(), outfile);
    QPixmap p = QPixmap(outfile);
    qDebug() << p.size();
    p.save(QDir::fromNativeSeparators(s_fileName));
    return 0;
}
